package thread2;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class DownLoadImgByThread extends Thread {
    private final String url;
    private final String name;

    public DownLoadImgByThread(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.download(url, name);
        System.out.println("下载了文件名称为" + name);
    }

    public static void main(String[] args) {
        DownLoadImgByThread thread1 = new DownLoadImgByThread("https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF", "01.gif");
        DownLoadImgByThread thread2 = new DownLoadImgByThread("https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF", "02.gif");
        DownLoadImgByThread thread3 = new DownLoadImgByThread("https://t7.baidu.com/it/u=737555197,308540855&fm=193&f=GIF", "03.gif");

        thread1.start();
        thread2.start();
        thread3.start();
    }


    public static class WebDownLoader {
        public void download(String url, String name) {
            try {
                FileUtils.copyURLToFile(new URL(url), new File(name));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("file not find or url not find");
            }
        }
    }
}
